// store/modules/cesium.js

const state = {
    cesiumViewer: null, // 用于存储Cesium实例
  };
  
  const mutations = {
    setCesiumViewer(state, viewer) {
      state.cesiumViewer = viewer;
    },
    clearCesiumViewer(state) {
      state.cesiumViewer = null;
    },
  };
  
  const actions = {
    initializeCesium({ commit }) {
      // 如果已经初始化，则不重复初始化
      if (this.state.cesium.cesiumViewer) return;
  
      // 初始化Cesium viewer
      const viewer = new Xmap.Viewer('cesiumContainer', {
        geocoder: false,
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false,
        navigationHelpButton: false,
        animation: false,
        fullscreenButton: false,
        timeline: false,
      }).viewer;
  
      // 隐藏水印信息
      viewer.scene.frameState.creditDisplay.container.style.display = 'none';
  
      // 配置底图
      Xmap.AirspaceManage.HierarchicalGrid.createImageryProvider('xyz', {
        url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
      });
  
      // 设置鼠标滚轮事件类型为视图缩放
      viewer.scene.screenSpaceCameraController.zoomEventTypes = [
        Cesium.CameraEventType.WHEEL,
      ];
  
      // 设置鼠标中键按下事件类型为视角倾斜
      viewer.scene.screenSpaceCameraController.tiltEventTypes = [
        Cesium.CameraEventType.MIDDLE_DRAG,
      ];
  
      // 设置初始视角位置
      viewer.camera.setView({
        destination: Cesium.Cartesian3.fromDegrees(113.5405629, 22.1247559, 17000),
      });
  
      // 将初始化后的viewer存入state
      commit('setCesiumViewer', viewer);
  
      console.log('Cesium初始化完成');
    },
  
    clearCesiumInstance({ commit }) {
      commit('clearCesiumViewer');
      console.log('Cesium实例已清除');
    },
  };
  
  const getters = {
    cesiumViewer: (state) => state.cesiumViewer,
  };
  
  export default {
    state,
    mutations,
    actions,
    getters,
  };
  